// app = angular.module('moGu');
app.controller('homeController',['$scope','$http','$timeout','$css', '$interval', '$rootScope',function($scope, $http,$timeout,$css, $interval, $rootScope){
  $css.add('home/css/home.css')

  $rootScope.addHomeGoods = function () {
    $(".homeMotai").css("display","block")
    $scope._this = this;
    $(".motaiImg img").attr("src",this.love.img);
    $(".motaiP").text(this.love.title);
    $(".motaiPrice").text('￥'+this.love.price)


    $(".back1").on("click",function(){
      $(".motaiBox").css("display",'none');
      $(".count").text(1)
      $(".homeMotai").css("display","none");

    })
    $(".backCar").on("click",function(){
      $(".motaiBox").css("display",'none');
      $(".homeMotai").css("display","none")
    window.location.replace('#/car')
    })

      $(".motaiCar").on("click",function(){
        $(".motaiBox").css("display",'block')

      })

        var sizeFlag = "";
        $('.size').on("click",function(){
        $('.size').each(function(index3,ele3){
            $(this).css("background-color",'#fff')
        })
          $(this).css("background-color",'red');
          sizeFlag = $(this).text();
          console.log(sizeFlag);
        })

      $(".jian").on("click",function(){
        // console.log($(this));
        num--;
        if(num<=0){
            num = 0;
        }
        $(this).next().text(num);
      })
      var num = 1;
      $(".jia").on("click",function(){

        num++;
        $(this).prev().text(num);
      })

      //——————————————————————————————————————————————————————
      $(".sure").on("click",function(){
        var $count = $('.count').text();
        console.log(sizeFlag);
        if(sizeFlag == ""){
          alert("请选择尺码");
        }else if($count <= 0) {
          alert("请选择数量");
        }
        $rootScope.homeArr = [];
        var obj={};
        var count =1;


        // 浏览量
        obj.count = $(".count").text();
        // 赞扬
        obj.praise = $scope._this.$$watchers[0].last;
        // 价格
        obj.price = $scope._this.$$watchers[1].last;
        // 图片
        obj.img = $scope._this.$$watchers[3].last;
        // 销量
        obj.sale = $scope._this.$$watchers[2].last;
        // 标题
        obj.title = $scope._this.love.title;
        $rootScope.homeArr.push(obj);

        console.log(obj);
      })



  }

  $('.home_headerImg1').on('click',function(){
    window.location.replace("#/login");
    $("#mine").trigger("click")
  })
  $('.home_headerImg2').on('click',function() {
    window.location.replace("#/car")
  })

  $http({
              method: 'GET',
              url: './home/json/home1.json'
          }).then(function (obj) {
              console.log(obj.data.data);
              // **********轮播图*********************************
              $scope.lists = obj.data.data[36344].list;

              $timeout(function () {
                var mySwiper = new Swiper ('.swiper-container', {
                autoplayDisableOnInteraction : false, //实现继续播放
                autoplay: 1000,                     //实现自动播放
                loop:true,                        //实现环路播放
                pagination: '.swiper-pagination',
                observer: true,
  			        observeParents: true

              });

            });


              // **************人气热销**********************************8
              $scope.list = obj.data.data[40298].list;
              $scope.hots0 = [$scope.list[0],$scope.list[3]];
              $scope.hots1 = [$scope.list[1],$scope.list[2],$scope.list[4],$scope.list[5]];
              console.log($scope.hots1)
            // ****************团购***************************
              $scope.tuans = obj.data.data[41161].list;

              // window.onload = function(){
              $timeout(function(){
                var img1 = document.createElement("img");
                img1.src = "http://www.2cto.com/uploadfile/2013/0219/20130219023101443.gif";
                var divs=angular.element(document.querySelector('.img2'));
                // divs.append(img1);
                console.log(divs);
                var img2 = new Image();
                img2.onload = function(){
                  img1.src = this.src;
                }
                console.log("{{tuan.image}}");
                img2.src = "{{tuan.image}}"
              })

              // }
              // *****************抢购****************************
              $scope.qiangs = obj.data.data[41119].list;
              $scope.qiangGou = parseInt($scope.qiangs[0].title);
              $scope.fasts = $scope.qiangs[0].list;


                // 当前时间的时间戳
                var getHours = new Date();
                $scope.oldTime = getHours.getTime();
                console.log(getHours);
                console.log($scope.oldTime);

                getHours.setHours($scope.qiangGou-8);
                console.log($scope.qiangGou);
                getHours.setMinutes(0);
                getHours.setSeconds(0);hais
                $scope.newTime = getHours.getTime();
                console.log(getHours);

                $scope.time =  $scope.newTime - $scope.oldTime;

                  console.log($scope.qiangGou);
                  console.log($scope.oldTime);
                  console.log($scope.newTime);
                  console.log($scope.time);


                  $one_minute = $interval(function(){
                      $scope.time -= 1000;
                  },1000);


          },function (err) {
              console.log(err);
          })

// **************************精选专题*****************************
          $http({
            method:'GET',
            url:'./home/json/home2.json'
          }).then(function(obj){
            // console.log(obj.data.data.list);
            $scope.zhuanTis=obj.data.data.list;
          },function(err){
            console.log(err)
          })


          // *****************猜你喜欢*************************************
          $http({
            method:'GET',
            url:'./home/json/home3.json'
          }).then(function(obj){
            // console.log(obj.data.result.wall);
            $scope.like = obj.data.result.wall.title;
            $scope.loves = obj.data.result.wall.docs;

          },function(err){
            console.log(err)
          })



          $http({
            method:'GET',
            url:'./home/json/home4.json'
          }).then(function(obj){
            // console.log(obj.data.result.wall);
            $scope.loves2 = obj.data.result.wall.docs;
          },function(err){
            console.log(err)
          })


          $http({
            method:'GET',
            url:'./home/json/home5.json'
          }).then(function(obj){
            // console.log(obj.data.result.wall);
            $scope.loves3 = obj.data.result.wall.docs;
          },function(err){
            console.log(err)
          })

          $http({
            method:'GET',
            url:'./home/json/home6.json'
          }).then(function(obj){
            // console.log(obj.data.result.wall);
            $scope.loves4 = obj.data.result.wall.docs;
          },function(err){
            console.log(err)
          })


}])

//==========================================
